package com.cysion.study.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.CommentsAdapter
import com.cysion.study.entity.CommentsEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_comments.*
import kotlinx.android.synthetic.main.activity_comments.multiView
import kotlinx.android.synthetic.main.activity_comments.smartLayout
import kotlinx.android.synthetic.main.activity_comments.topbar
import kotlinx.android.synthetic.main.activity_know_list.*

class CommentsActivity : BaseActivity(){


    override fun getLayoutId(): Int = R.layout.activity_comments


    private lateinit var proAdapter: CommentsAdapter
    private val noticeList: MutableList<CommentsEntity> = mutableListOf()
    private var curPage = 1

    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("留言列表")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()
    }
    //加载数据
    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList(curPage)
            smartLayout.setEnableLoadMore(true)
        }
        smartLayout.setOnLoadMoreListener {
            getDataList(curPage)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rvBloglist.isNestedScrollingEnabled = false
        proAdapter = CommentsAdapter(noticeList, Box.context)
        rvBloglist.adapter = proAdapter
        rvBloglist.layoutManager = LinearLayoutManager(Box.context)
        proAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {
                var bundle = Bundle()
                bundle.putString("fromUserId",obj.fromUserId)
                startActivity_ex<CommentsDetailActivity>("key", bundle)
            }
        }

    }

    private fun getDataList(page: Int) {
        Alert.loading(self)
        ApiClient.api.getChatList(UserCache.token,curPage.toString()).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    if (curPage == 1) {
                        noticeList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        noticeList.addAll(it)
                        proAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }
    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    override fun closeMvp() {
    }

}
